/*
 * Copyright 2011 Impetus Infotech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.impetus.teamplan.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.impetus.teamplan.Constants;
import com.impetus.teamplan.entity.Accomplishment;
import com.impetus.teamplan.entity.Member;
import com.impetus.teamplan.entity.Responsibility;
import com.impetus.teamplan.utils.KunderaUtils;
import com.impetus.teamplan.web.WebUtils;

/**
 * <Provide Type description>
 * @author amresh.singh
 */
public class TeamPlanDaoImpl implements TeamPlanDao {
	private static EntityManager em;	
	
	
	@Override
	public List<Member> getAllMembers() {		
	
		em = KunderaUtils.getEntityManager();	
		Query q = em.createQuery("select m from Member m");
		List<Member> members = q.getResultList();		
		System.out.println("A total of " + members.size() + " members found");			
		KunderaUtils.closeEntityManager(em);	
			
		return members;			
	}
	
	@Override
	public Member getMemberById(String memberId) {	
		System.out.println("Finding member whose ID is " + memberId);
		em = KunderaUtils.getEntityManager();	
		System.out.println("Entity Manager: " + em);
       
        Member member = null; 
        member = em.find(Member.class, memberId);
        if(member == null) {
        	System.out.println("No Member found");        	
        } else {
        	System.out.println("Found member " + member);        	
        } 
        KunderaUtils.closeEntityManager(em);		  
        return member;
           
	}
	
	public void addOrUpdateMember(Member member) {
		em = KunderaUtils.getEntityManager();
		em.persist(member);      
        System.out.println("Member Successfully updated");        
        KunderaUtils.closeEntityManager(em);	
	}
	
	public void deleteMember(Member member) {
		em = KunderaUtils.getEntityManager();	
		System.out.println("Member to be deleted is " + member.getMemberId());
		
		em.remove(member);		
        System.out.println("Member Successfully deleted");       
        KunderaUtils.closeEntityManager(em);	
	}

	
	public void addMember() throws Exception {		
        
		em = KunderaUtils.getEntityManager();
		Member member1 = new Member();
		
        
        member1.setMemberId("IIIPL-0001");
        member1.setMemberName("Administrator");
        member1.setPassword("aaaa");
        member1.setMemberType(Constants.MEMBER_TYPE_ADMIN);
        
        List<Responsibility> responsibilities1 = new ArrayList<Responsibility>();
        responsibilities1.add(new Responsibility(WebUtils.getUniqueId(), "Kundera"));
        responsibilities1.add(new Responsibility(WebUtils.getUniqueId(), "Cassandra"));
        member1.setResponsibilities(responsibilities1);
        
        List<Accomplishment> accomplishments1 = new ArrayList<Accomplishment>(); 
        
        ArrayList<String> acc1 = new ArrayList<String>();
        acc1.add("Design Complete");
        acc1.add("Coding Done");
        
        ArrayList<String> acc2 = new ArrayList<String>();
        acc2.add("Test case prepared");
        acc2.add("Deployment done");
        
        accomplishments1.add(new Accomplishment(WebUtils.getUniqueId(), "0", "2011", acc1));
        accomplishments1.add(new Accomplishment(WebUtils.getUniqueId(), "1", "2011", acc2));
        member1.setAccomplishments(accomplishments1);    
                
        
        em.persist(member1);  
        KunderaUtils.closeEntityManager(em);
        
        System.out.println("Admin Persisted");    

	}
	
	public static void main(String args[]) throws Exception {
		TeamPlanDao teamPlanDao = new TeamPlanDaoImpl();
		teamPlanDao.addMember();
		
		
		/*List<Member> members = teamPlanDao.getAllMembers();
		Member member = members.get(0);
		//member.setMemberName(member.getMemberName() + "2");
		List<Accomplishment> accs = member.getAccomplishments();
		List<String> list = new ArrayList<String>();
		list.add("CCCC");
		list.add("DDDD");
		accs.add(new Accomplishment(WebUtils.getUniqueId(), "7", "2011", list));
		member.setAccomplishments(accs);
		teamPlanDao.updateMember(member);*/
		//List<Member> members = teamPlanDao.getAllMembers();
		//System.out.println(members);
		//Member member = teamPlanDao.getMemberById("IIIPL-0001");
		//System.out.println("Got member by Id " +  member);
		
		
		System.exit(0);
		
	}
}
